import { createRouter, createWebHashHistory } from 'vue-router';

import routes from './routes'

let router = createRouter({
  routes,
  history: createWebHashHistory(),
  // 控制页面切换时 滚动条位置
  scrollBehavior: (to, from, savedPosition) => savedPosition || { top: 0 },
});
// 路由拦截
// 全局前置守卫
router.beforeEach((to, from, next) => {
  // next()代表放行
  // next('/login')表示拦截至登录页

  // 判断是否需要做登陆验证
  if (to.meta.isLogin) {
    if (localStorage.getItem('token')) {
      next();
    } else {
      next('/login')
    }
  } else {
    next();//放行
  }
});

export default router;